﻿using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;

namespace SBlogCore.Common.DatabaseAccessor.Repositorys.EFCore
{
    public partial class Repository<TEntity> where TEntity : class, IBaseEntity
    {

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.Where(predicate);
        }
        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public TEntity? FirstOrDefault(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.FirstOrDefault(predicate);
        }

        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <returns></returns>
        public TEntity? FirstOrDefault()
        {
            return Entities.FirstOrDefault();
        }
        /// <summary>
        /// Id查找数据
        /// </summary>
        /// Find会优先查询缓存
        /// <returns></returns>
        public TEntity? FindById(long id)
        {
            return Entities.Find(id);
        }
        /// <summary>
        /// 异步Id查找数据 
        /// </summary>
        /// Find会优先查询缓存
        /// <returns></returns>
        public ValueTask<TEntity?> FindByIdAsync(long id)
        {
            return Entities.FindAsync(id);
        }
        /// <summary>
        /// 异步查询第一个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.FirstOrDefaultAsync(predicate);
        }
        public Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.FirstAsync(predicate);
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.AnyAsync(predicate);
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public bool Any(Expression<Func<TEntity, bool>> predicate)
        {
            return Entities.Any(predicate);
        }

        /// <summary>
        /// 异步查询第一个
        /// </summary>
        /// <returns></returns>
        public Task<TEntity?> FirstOrDefaultAsync()
        {
            return Entities.FirstOrDefaultAsync();
        }
        /// <summary>
        /// 执行原生SQL 
        /// </summary>
        /// <param name="formatTable"></param>
        /// <returns></returns>
        public IQueryable<TEntity> FromSqlInterpolated(FormattableString formatTable)
        {
            var res = Entities.FromSqlInterpolated(formatTable);
            return res;
        }

        public IQueryable<TEntity> Query<T>(string sql, params object[] parameters)
        {
            return Entities.FromSqlRaw(sql, parameters);
        }
        /// <summary>
        /// 分页查询，排序待加  orderBy未解决
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageindex">页码</param>
        /// <param name="pageSize">每页显示多少条，等于0时不分页</param>
        /// <param name="orderBy">排序</param>
        /// <param name="sql">sql语句</param>
        /// <param name="count">总数</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public IQueryable<TEntity> Query<T>(int pageindex,int pageSize,string orderBy,string sql,ref int count, params object[] parameters) where T:class
        {
            IQueryable<TEntity> queryable;
            if(pageSize==0)
            {
                if (!string.IsNullOrEmpty(orderBy))
                    //IQueryable = Entities.FromSqlRaw(sql, parameters).orderBy(orderBy);
                    queryable = Entities.FromSqlRaw(sql, parameters);

                else
                    queryable = Entities.FromSqlRaw(sql, parameters);
            }
            else
            {
                count = Entities.FromSqlRaw(sql, parameters).Count();
                if (!string.IsNullOrEmpty(orderBy))
                    queryable = Entities.FromSqlRaw(sql, parameters).OrderBy("");
                else
                    queryable = Entities.FromSqlRaw(sql, parameters).Skip(pageSize * (pageindex - 1)).Take(pageSize);
            }
            return queryable;
        }
    }
}
